﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace POS_NEW
{
    public partial class frmPhieuXoaSua : Form
    {
        public frmPhieuXoaSua()
        {
            InitializeComponent();
        }
        private string ma = "";

        object TuNgay = null;
        object DenNgay = null;
        #region "Load data"
        private void LoadgridPhieu(string ma,string tenDoiTuong,object TuNgay,object DenNgay)
        {
            gridPhieu.ResetDisplayLayout();
            gridPhieu.DataSource=AccessDataLayer.ExecuteDT("spPhieu_XoaSua_Select",CommandType.StoredProcedure,
                "@ma",ma,
                "@Ten_NhanVien",tenDoiTuong,
                "@TuNgay",TuNgay,
                "@DenNgay",DenNgay
                );
            gridPhieu.DisplayLayout.Bands[0].Columns["Ngày lập phiếu"].Format = "dd/MM/yyyy";
            gridPhieu.DisplayLayout.Bands[0].Columns["Ngày xóa sửa"].Format = "dd/MM/yyyy";
            gridPhieu.DisplayLayout.Override.AllowDelete = Infragistics.Win.DefaultableBoolean.False;
            btnRestore.Enabled = false;
            gridPhieu_Chitiet.DataSource = null;
        }
        private void LoadgridPhieu_Chitiet()
        {
            if(gridPhieu.ActiveRow!=null)
            {
                gridPhieu_Chitiet.DataSource = AccessDataLayer.ExecuteDT("spPhieu_XoaSua_Chitiet_Select",CommandType.StoredProcedure,
                    "@Ma_Phieu",gridPhieu.Rows[gridPhieu.ActiveRow.Index].Cells["Mã phiếu"].Value.ToString()
                    );
                gridPhieu_Chitiet.DisplayLayout.Bands[0].Columns["Đơn giá"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;
                gridPhieu_Chitiet.DisplayLayout.Bands[0].Columns["Thành tiền"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;
                gridPhieu_Chitiet.DisplayLayout.Override.AllowDelete = Infragistics.Win.DefaultableBoolean.False;
            }
        }
        #endregion
        #region "Restore data"
        private int RestoreData()
        {
            int kq = AccessDataLayer.ExecuteNonQuery("spPhieu_XoaSua_Restore",CommandType.StoredProcedure,
                                                        "@Ma_Phieu",gridPhieu.Rows[gridPhieu.ActiveRow.Index].Cells[0].Value);
            return kq;
        }
        #endregion
        #region "Other methods"
        #endregion
        private void frmPhieuXoaSua_Load(object sender, EventArgs e)
        {
           // this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Maximized;
            dtTuNgay.Select();
            dtTuNgay.SelectAll();
            ma="DHX";
            this.LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
            gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = false;
            gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = true;
        }
        private void frmPhieuXoaSua_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
                this.Close();
            else if (e.KeyData == Keys.F5)
                btSearchAll_Click(null, null);
            else if (e.KeyData == Keys.F8)
            {
                if(btnRestore.Enabled)
                btnRestore_Click(null, null);
            }
        }
        #region "gridPhieu"
        private void gridPhieu_BeforeCellActivate(object sender, Infragistics.Win.UltraWinGrid.CancelableCellEventArgs e)
        {
            e.Cell.Activation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;
        }
        private void gridPhieu_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e)
        {
            gridPhieu.DisplayLayout.PerformAutoResizeColumns(true, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand);
            //gridPhieu.DisplayLayout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
        }

        private void gridPhieu_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs e)
        {
            gridPhieu_ClickCell(null, null);

        }
        private void gridPhieu_ClickCell(object sender, Infragistics.Win.UltraWinGrid.ClickCellEventArgs e)
        {
            if (gridPhieu.Rows.Count > 0)
            {
                if (gridPhieu.ActiveRow != null)
                {
                    if (gridPhieu.Rows[gridPhieu.ActiveRow.Index].Cells["Tình trạng"].Text == "Đã xóa")
                    btnRestore.Enabled = true;
                    else
                        btnRestore.Enabled=false;
                    gridPhieu.DisplayLayout.Rows[gridPhieu.DisplayLayout.ActiveRow.Index].Selected = true;
                    gridPhieu.DisplayLayout.Override.SelectTypeRow = Infragistics.Win.UltraWinGrid.SelectType.Single;
                    LoadgridPhieu_Chitiet();
                }
            }
        }
        #endregion
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void gridPhieu_Chitiet_BeforeCellActivate(object sender, Infragistics.Win.UltraWinGrid.CancelableCellEventArgs e)
        {
            e.Cell.Activation = Infragistics.Win.UltraWinGrid.Activation.NoEdit; 
        }
        private void gridPhieu_Chitiet_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e)
        {
            gridPhieu_Chitiet.DisplayLayout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
            gridPhieu_Chitiet.DisplayLayout.PerformAutoResizeColumns(true, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand);

        }
        private void ulEx1_ItemClick(object sender, Infragistics.Win.UltraWinExplorerBar.ItemEventArgs e)
        {
            dtTuNgay.DateTime = DateTime.Today;
            dtDenNgay.DateTime = DateTime.Today;
            dtTuNgay.Select();
            dtTuNgay.SelectAll();
            //ulEx1.Groups["DonHang"].Items["KhachHang"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["DonHang"].Items["KhachHang"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["DonHang"].Items["NhaCungCap"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["DonHang"].Items["NhaCungCap"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["ThuChi"].Items["PhieuThu"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["ThuChi"].Items["PhieuThu"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["ThuChi"].Items["PhieuChi"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["ThuChi"].Items["PhieuChi"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["NhapXuat"].Items["TamNhap"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["NhapXuat"].Items["TamNhap"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["NhapXuat"].Items["PhieuNhap"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["NhapXuat"].Items["PhieuNhap"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["NhapXuat"].Items["TamXuat"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["NhapXuat"].Items["TamXuat"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["NhapXuat"].Items["PhieuXuat"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["NhapXuat"].Items["PhieuXuat"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["TraHang"].Items["KhachHang"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["TraHang"].Items["KhachHang"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            //ulEx1.Groups["TraHang"].Items["NhaCungCap"].Settings.AppearancesSmall.Appearance.ResetBackColor();
            //ulEx1.Groups["TraHang"].Items["NhaCungCap"].Settings.AppearancesSmall.Appearance.ResetForeColor();
            if (ulEx1.Groups["DonHang"].Items["KhachHang"].Active)
            {
                //ulEx1.Groups["DonHang"].Items["KhachHang"].Settings.AppearancesSmall.Appearance.BackColor= Color.DeepSkyBlue;
                //ulEx1.Groups["DonHang"].Items["KhachHang"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                ultraGrbPhieu.Text = "ĐƠN HÀNG KHÁCH HÀNG";
                ma="DHX";
                LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = false;
                gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = true;
            }
            else
                if (ulEx1.Groups["DonHang"].Items["NhaCungCap"].Active)
                {
                    //ulEx1.Groups["DonHang"].Items["NhaCungCap"].Settings.AppearancesSmall.Appearance.BackColor = Color.DeepSkyBlue;
                    //ulEx1.Groups["DonHang"].Items["NhaCungCap"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                    ultraGrbPhieu.Text = "ĐƠN HÀNG NHÀ CUNG CẤP";
                    ma="DHN";
                    LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                    gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = true;
                    gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = false;
                }
            else
                if (ulEx1.Groups["ThuChi"].Items["PhieuThu"].Active)
                {
                    //ulEx1.Groups["ThuChi"].Items["PhieuThu"].Settings.AppearancesSmall.Appearance.BackColor=Color.DeepSkyBlue;
                    //ulEx1.Groups["ThuChi"].Items["PhieuThu"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                    ultraGrbPhieu.Text = "PHIẾU THU";
                    ma="PTH";
                    LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                    gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = false;
                    gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = false;
                }
            else
                if (ulEx1.Groups["ThuChi"].Items["PhieuChi"].Active)
                {
                    //ulEx1.Groups["ThuChi"].Items["PhieuChi"].Settings.AppearancesSmall.Appearance.BackColor=Color.DeepSkyBlue;
                    //ulEx1.Groups["ThuChi"].Items["PhieuChi"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                    ma = "PCH";
                    LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                    ultraGrbPhieu.Text = "PHIẾU CHI";
                    gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = false;
                    gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = false;
                }
            else
                if (ulEx1.Groups["NhapXuat"].Items["TamNhap"].Active)
                {
                    ma = "PNT";
                    //ulEx1.Groups["NhapXuat"].Items["TamNhap"].Settings.AppearancesSmall.Appearance.BackColor=Color.DeepSkyBlue;
                    //ulEx1.Groups["NhapXuat"].Items["TamNhap"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                    LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                    ultraGrbPhieu.Text = "PHIẾU NHẬP TẠM";
                    gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = true;
                    gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = false;
                }
            else
                if (ulEx1.Groups["NhapXuat"].Items["PhieuNhap"].Active)
                {
                    ma = "PNC";
                    //ulEx1.Groups["NhapXuat"].Items["PhieuNhap"].Settings.AppearancesSmall.Appearance.BackColor = Color.DeepSkyBlue;
                    //ulEx1.Groups["NhapXuat"].Items["PhieuNhap"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                    LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                    ultraGrbPhieu.Text = "PHIẾU NHẬP";
                    gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = true;
                    gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = false;
                }
            else
                if (ulEx1.Groups["NhapXuat"].Items["TamXuat"].Active)
                {
                    ma = "PXT";
                    //ulEx1.Groups["NhapXuat"].Items["TamXuat"].Settings.AppearancesSmall.Appearance.BackColor = Color.DeepSkyBlue;
                    //ulEx1.Groups["NhapXuat"].Items["TamXuat"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                    LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                    ultraGrbPhieu.Text = "PHIẾU XUẤT TẠM";
                    gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = false;
                    gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = true;
                }
            else
                 if (ulEx1.Groups["NhapXuat"].Items["PhieuXuat"].Active)
                {
                    ma = "PXC";
                    //ulEx1.Groups["NhapXuat"].Items["PhieuXuat"].Settings.AppearancesSmall.Appearance.BackColor = Color.DeepSkyBlue;
                    //ulEx1.Groups["NhapXuat"].Items["PhieuXuat"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                    LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                    ultraGrbPhieu.Text = "PHIẾU XUẤT";
                    gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = false;
                    gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = true;
                }
            else
                  if (ulEx1.Groups["TraHang"].Items["KhachHang"].Active)
                     {
                         ultraGrbPhieu.Text = "PHIẾU TRẢ KHÁCH HÀNG";
                         ma = "PTK";
                         //ulEx1.Groups["TraHang"].Items["KhachHang"].Settings.AppearancesSmall.Appearance.BackColor = Color.DeepSkyBlue;
                         //ulEx1.Groups["TraHang"].Items["KhachHang"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                         LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                         gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = false;
                         gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = true;
                     }
            else
                 if (ulEx1.Groups["TraHang"].Items["NhaCungCap"].Active)
                    {
                        ultraGrbPhieu.Text = "PHIẾU TRẢ NHÀ CUNG CẤP";
                        ma = "PTN";
                        //ulEx1.Groups["TraHang"].Items["NhaCungCap"].Settings.AppearancesSmall.Appearance.BackColor = Color.DeepSkyBlue;
                        //ulEx1.Groups["TraHang"].Items["NhaCungCap"].Settings.AppearancesSmall.Appearance.ForeColor = Color.White;
                        LoadgridPhieu(ma, "", dtTuNgay.Value, dtDenNgay.Value);
                        gridPhieu.DisplayLayout.Bands[0].Columns["Khách hàng"].Hidden = true;
                        gridPhieu.DisplayLayout.Bands[0].Columns["Nhà cung cấp"].Hidden = false;
                    }
        }

        private void btSearchAll_Click(object sender, EventArgs e)
        {
            TuNgay = null;
            DenNgay = null;
            LoadgridPhieu(ma, "", TuNgay, DenNgay);
        }

        private void dtTuNgay_Enter(object sender, EventArgs e)
        {
            dtTuNgay.SelectAll();
        }

        private void dtDenNgay_Enter(object sender, EventArgs e)
        {
            dtDenNgay.SelectAll();
        }

        private void txtNhanVien_Enter(object sender, EventArgs e)
        {
            txtNhanVien.SelectAll();
        }

        private void dtTuNgay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                dtDenNgay.Focus();
        }

        private void dtDenNgay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                txtNhanVien.Focus();
        }

        private void dtTuNgay_ValueChanged(object sender, EventArgs e)
        {
            TuNgay = dtTuNgay.Value;
            DenNgay = dtDenNgay.Value;
            LoadgridPhieu(ma, txtNhanVien.Text, TuNgay, DenNgay);
        }

        private void dtDenNgay_ValueChanged(object sender, EventArgs e)
        {
            TuNgay = dtTuNgay.Value;
            DenNgay = dtDenNgay.Value;
            LoadgridPhieu(ma, txtNhanVien.Text, TuNgay, DenNgay);
        }

        private void txtNhanVien_ValueChanged(object sender, EventArgs e)
        {
            LoadgridPhieu(ma, txtNhanVien.Text, TuNgay, DenNgay);
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {

            using (ConfirmScreen cfs = new ConfirmScreen("Bạn có muốc khôi phục lại phiếu \n" + gridPhieu.Rows[gridPhieu.ActiveRow.Index].Cells[0].Value.ToString()+" ?"))
            {
                if (cfs.ShowDialog()==DialogResult.OK)
                {
                    try
                    {
                        if (RestoreData() > 0)
                        {
                            using (ErrorScreen ers = new ErrorScreen("Đã khôi phục phiếu \n" + gridPhieu.Rows[gridPhieu.ActiveRow.Index].Cells[0].Text, "Thông báo ", 3))
                            {
                                ers.ShowDialog();
                                gridPhieu.DisplayLayout.Override.AllowDelete = Infragistics.Win.DefaultableBoolean.True;
                                gridPhieu.DeleteSelectedRows(false);
                                gridPhieu_Chitiet.DataSource = null;
                                btnRestore.Enabled = false;
                                gridPhieu_Chitiet.DisplayLayout.Override.AllowDelete = Infragistics.Win.DefaultableBoolean.False;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        gridPhieu_Chitiet.DisplayLayout.Override.AllowDelete = Infragistics.Win.DefaultableBoolean.False;
                        using (ErrorScreen ers1 = new ErrorScreen(ex.Message))
                            ers1.ShowDialog();
                    }
                }
                else
                    return;
            }
        }
    }
}
